<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class connect_mod extends module
{
    function __construct()
    {
        $this->uid = $_SESSION['user']['uid'];
        parent::__construct();
    }
    function qq($code, $state)
    {
        $ct = new qq();
        $data = $ct->getAccessToken($code, $state);
        $info = $ct->getUserInfo($data['access_token'], $data['openid']);
        $row = $this->db->get_one("select * from " . table('member') . " where qq_uid = '" . $data['openid'] . "'");
        $_SESSION['connect']['qq']['nick'] = $info['nickname'];
        $_SESSION['connect']['qq']['qq_uid'] = $data['openid'];
        $_SESSION['connect']['qq']['sex'] = $info['gender'];
        $_SESSION['connect']['qq']['avatar'] = $info['figureurl_1'];
        if (empty($this->uid)) {
            if (empty($row['uid'])) {
                $data['nick'] = $info['nickname'];
                $data['qq_uid'] = $data['openid'];
                $uid = $this->module("member")->register_user_connect($data);
                $this->module('member')->user_session($uid);
                showmsg('创建帐户,登录成功', 'member.php');
            } else {
                $this->module('member')->user_session($row);
                showmsg('登录成功', 'member.php');
            }
        } else {
            if (empty($row['qq_uid'])) {
                if (empty($row['nick'])) {
                    $up_data['nick'] = $info['nickname'];
                }
                $up_data['qq_uid'] = $data['openid'];
                $query = $this->db->update(table('member'), $up_data, 'uid = ' . $this->uid);
                if ($query) {
                    showmsg('绑定成功', 'member.php?m=user&a=connect');
                } else {
                    showmsg('绑定失败', 'member.php?m=user&a=connect');
                }
            } else {
                showmsg('已经绑定，无需再绑定', 'member.php?m=user&a=connect');
            }
        }
    }
    function weibo($code)
    {
        $wb = new weibo();
        $api_data = $wb->getAccessToken($code);
        $info = $wb->getUserInfo($data['access_token'], $api_data['uid']);
        $_SESSION['connect']['weibo']['nick'] = $info->name;
        $_SESSION['connect']['weibo']['weibo_uid'] = $api_data['uid'];
        $_SESSION['connect']['weibo']['sex'] = $info->gender;
        $_SESSION['connect']['weibo']['avatar'] = $info->avatar_large;
        $row = $this->db->get_one("select * from " . table('member') . " where weibo_uid = '" . $api_data['uid'] . "'");
        if (empty($this->uid)) {
            if (empty($row['uid'])) {
                $udata['nick'] = $info->name;
                $udata['weibo_uid'] = $api_data['uid'];
                $uid = $this->module("member")->register_user_connect($udata);
                $this->module('member')->user_session($uid);
                showmsg('创建帐户,登录成功', 'member.php');
            } else {
                $this->module('member')->user_session($row['uid']);
                showmsg('登录成功', 'member.php');
            }
        } else {
            if (empty($row['weibo_uid'])) {
                if (empty($row['nick'])) {
                    $udata['nick'] = $info->name;
                }
                $udata['weibo_uid'] = $api_data['uid'];
                $query = $this->db->update(table('member'), $udata, 'uid = ' . $this->uid);
                if ($query) {
                    showmsg('绑定成功', 'member.php?m=user&a=connect');
                } else {
                    showmsg('绑定失败', 'member.php?m=user&a=connect');
                }
            } else {
                showmsg('已经绑定，无需再绑定', 'member.php?m=user&a=connect');
            }
        }
    }
    function wchat($code, $state)
    {
        $wt = new wchat();
        $data = $wt->getAccessToken($code, $state);
        $row = $this->db->get_one("select * from " . table('member') . " where wchat_uid = '" . $data['openid'] . "'");
        if (empty($this->uid)) {
            if (empty($row['uid'])) {
                $data['nick'] = $info['nickname'];
                $data['wchat_uid'] = $data['openid'];
                $data['register_ip'] = get_client_ip();
                $data['addtime'] = time();
                $this->db->add(table('member'), $data);
                $insert_id = $this->db->insert_id();
                $this->module('member')->user_session($insert_id);
                showmsg('登录成功', 'member.php');
            } else {
                $this->module('member')->user_session($row['uid']);
            }
        } else {
            if (empty($row['wchat_uid'])) {
                if (empty($row['nick'])) {
                    $data['nick'] = $info['nickname'];
                }
                $data['wchat_uid'] = $data['openid'];
                $query = $this->db->update(table('member'), $data, 'uid = ' . $this->uid);
                if ($query) {
                    showmsg('绑定成功', 'member.php?m=user&a=connect');
                }
            } else {
                showmsg('已经绑定，无需再绑定');
            }
        }
    }
}